﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Viettel.VOFFICE.DAO.Models;
using Viettel.VOFFICE.Web.Helpers;
using Viettel.VOFFICE.util;

namespace Viettel.VOFFICE.Web.Models
{
    public class NodeModel : BaseModel
    {
        public NodeModel() { }
        public NodeModel(Node node)
        {
            Id = node.Id;
            Name = node.Name;
            Description = node.Description;
            FlowId = node.FlowId;
            RoleId = node.RoleId;
            IsDocNode = node.IsDocNode;
            IsStart = node.IsStart;
            CreatedAt = node.CreatedAt;
            UpdatedAt = node.UpdatedAt;
            CreatorId = node.CreatorId;
            if (node.RoleId != null)
            {
                RoleName = node.aspnet_Roles.RoleName;
            }
            if (node.NodeRelations1 != null && node.NodeRelations1.Any())
            {
                NextNodeList = node.NodeRelations1.Select(o => o.ToNodeId).ToList();
            }

            // lấy ra người dùng trong node đó
            if (node.NodeUsers != null && node.NodeUsers.Any())
            {
                UserInNode = node.NodeUsers.Select(o => new PickerModel()
                    {
                        Key = o.UserId,
                        Value = ProfileHelpers.GetUserProfile(o.aspnet_Users.UserName).FullName,
                        Selected = o.MainExecutor
                    }).ToList();
            }
            else
            {
                // quan trọng, để tránh null cho code js
                UserInNode = new List<PickerModel>();
            }

            // lấy công việc của node đó
            if (node.Todos != null && node.Todos.Any())
            {
                TodoInNode = node.Todos.Select(o => new PickerModel()
                    {
                        Key = o.Id,
                        Value = o.Name,
                        Selected = o.IsCheckList
                    }).ToList();
            }
            else
            {
                // quan trọng, để tránh null cho code js
                TodoInNode = new List<PickerModel>();
            }
        }

        public int Id { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "region")]
        public string Name { get; set; }

        public string Description { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "region")]
        [Range(1, int.MaxValue, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "region")]
        public int FlowId { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "region")]
        public Guid? RoleId { get; set; }

        public string RoleName { get; set; }

        public List<int> NextNodeList { get; set; }
        public string NextNodeJson
        {
            get
            {
                return NextNodeList != null ? NextNodeList.ToJsonString() : string.Empty;
            }
        }

        public List<PickerModel> UserInNode { get; set; }
        public List<PickerModel> TodoInNode { get; set; }

        public bool IsDocNode { get; set; }
        public bool? IsStart { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        public Guid? CreatorId { get; set; }
    }
}